package com.example.myapplication3.audio;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.myapplication3.audio.bean.AudioItem;
import com.example.myapplication3.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AudioQueryHelper {
    private static final String TAG = "AudioQueryHelper";
    private static final ExecutorService executor = Executors.newFixedThreadPool(2);

    public static void asynLoadAllAudios(Context context, int pageIndex, int itemCount, AudioLoadCallback callback) {
        executor.execute(() -> {
            List<AudioItem> audios = new ArrayList<>();

            String[] projection = {
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DATE_ADDED,
                    MediaStore.Audio.Media.DATE_MODIFIED,
                    MediaStore.Audio.Media.MIME_TYPE
            };

            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
            String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC";
            Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

            try (Cursor cursor = context.getContentResolver().query(
                    mediaUri,
                    projection,
                    selection,
                    null,
                    sortOrder
            )) {
                if (cursor != null) {
                    cursor.moveToPosition(pageIndex * itemCount);
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                        String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                        String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                        String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                        String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                        long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                        long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                        long dateAdded = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_ADDED));
                        long dateModified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED));
                        String mime = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));

                        Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

                        AudioItem item = new AudioItem(id, title, artist, album, path, uri,
                                size, duration, dateAdded, dateModified, mime);
                        audios.add(item);

                        if (audios.size() >= itemCount) break;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, "获取音频失败", e);
                callback.onAudiosLoaded(null);
                return;
            }

            callback.onAudiosLoaded(audios);
        });
    }

    public interface AudioLoadCallback {
        void onAudiosLoaded(List<AudioItem> audios);
    }
}